package com.nosql.db.network;

import com.nosql.db.cluster.Node;
import com.nosql.db.core.modul.Document;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DBCommand implements Serializable {
    public enum Type { PUT, GET, DELETE, CREATE_COLLECTION,UPDATE, LEADER_QUERY, FLUSH, REPLICATION}
    private final Type type;
    private static final long serialVersionUID = 1L;

    protected DBCommand(Type type){
        this.type = type;
    }
     public Type getType() {
        return type;
    }
    public static class putCommand extends DBCommand{
        private final String collection;
        private final Map<String,Object> data; // 改为传输数据而非完整Document
        private final String id;
        public putCommand(String collection, Map<String,Object> data, String id) {
            super(Type.PUT);
            this.collection = collection;
            this.data = new ConcurrentHashMap<>(data);
            this.id = id;
        }


        public String getCollection() { return collection; }
        public String getId() { return id; }
        public Map<String, Object> getData() {
            return Collections.unmodifiableMap(data);
        }
    }

    public static class GetCommand extends DBCommand {
        private final String collection;
        private final String id;

        public GetCommand(String collection, String id) {
            super(Type.GET);
            this.collection = collection;
            this.id = id;
        }

        public String getCollection() { return collection; }
        public String getId() { return id; }
    }
     public static class DeleteCommand extends DBCommand {
        private final String collection;
        private final String id;

        public DeleteCommand(String collection, String id) {
            super(Type.DELETE);
            this.collection = collection;
            this.id = id;
        }

        public String getCollection() { return collection; }
        public String getId() { return id; }
    }
     public static class CreateCollectionCommand extends DBCommand {
        private final String collection;

        public CreateCollectionCommand(String collection) {
            super(Type.CREATE_COLLECTION);
            this.collection = collection;
        }

        public String getCollection() { return collection; }
    }
    public static class UpdateCommand extends DBCommand {
       private final String collection;
       private final String id;
       private final Map<String, Object> updateData;
        private final long expectedVersion;
       public UpdateCommand(String collection, String id, Map<String, Object> updateData) {
           super(Type.UPDATE);
           this.collection = collection;
           this.id = id;
           this.updateData=new ConcurrentHashMap<>(updateData);
           this.expectedVersion = (Long) updateData.getOrDefault("_version", -1L);
       }

        public String getCollection() {
            return collection;
        }

        public String getId() {
            return id;
        }
        public Map<String, Object> getUpdateData() {
            return Collections.unmodifiableMap(updateData);
        }
        // 新增版本获取方法
        public long getExpectedVersion() {
            return expectedVersion;
        }
    }

    // 心跳相关命令
    public static class HeartbeatPacket implements Serializable {
        private final String nodeId;
        private final long timestamp;

        public HeartbeatPacket(String nodeId, long timestamp) {
            this.nodeId = nodeId;
            this.timestamp = timestamp;
        }
    }

    public static class HeartbeatAck implements Serializable {
        private final String nodeId;

        public HeartbeatAck(String nodeId) {
            this.nodeId = nodeId;
        }
    }

    // 复制命令
    public static class ReplicationCommand extends DBCommand {
        private final DBCommand originalCommand;
        private final String sourceNodeId; // 来源节点ID

        public ReplicationCommand(String sourceNodeId, DBCommand command) {
            super(Type.REPLICATION);
            this.sourceNodeId = sourceNodeId;
            this.originalCommand = command;
        }

        public DBCommand getOriginalCommand() {
            return originalCommand;
        }

        public String getSourceNodeId() {
            return sourceNodeId;
        }
    }

    // 转发命令
    public static class ForwardingCommand extends DBCommand {
        private final String sourceNode;
        private final DBCommand originalCommand;

        public ForwardingCommand(String sourceNode, DBCommand command) {
            super(command.getType());
            this.sourceNode = sourceNode;
            this.originalCommand = command;
        }
    }

    public static class LeaderQuery extends DBCommand {
        public LeaderQuery() {
            super(Type.LEADER_QUERY);
        }
    }

    public static class LeaderResponse implements Serializable {
        private final Node leader; // 改为存储Node对象

        public LeaderResponse(Node leader) { // 参数类型改为Node
            this.leader = leader;
        }

        public Node getLeader() {
            return leader;
        }
    }

    public static class FlushCommand extends DBCommand {
        public FlushCommand() {
            super(Type.FLUSH);
        }
    }



}
